package com.lsj.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class VerifyCodeController {

    @Autowired
    Producer producer;



    @GetMapping("/verify_code")
    public void getVerifyCode(HttpServletResponse response, HttpSession httpSession) throws IOException {
        String text = producer.createText();
        httpSession.setAttribute("verify_code",text);
        BufferedImage image = producer.createImage(text);
        try(ServletOutputStream outputStream  = response.getOutputStream()){
            ImageIO.write(image,"jpg",outputStream);
        }
    }
}
